var gulp = require('gulp'),
    clean = require('gulp-clean'),
    watch = require('gulp-watch'),
    connect = require('gulp-connect'),
    sass = require('gulp-sass');

// 启动 webserver
gulp.task('webserver', function () {
    connect.server({
        port: 3000,
        root: 'dist/',
        livereload: true
    });
});

// html
gulp.task('html', function () {
    return gulp.src('src/**/*.html')
        .pipe(gulp.dest('dist'))
        .pipe(connect.reload());
})

// 编译 sass
gulp.task('sass', function () {
    return gulp.src('src/**/*.scss')
        .pipe(sass())
        .pipe(gulp.dest('dist'))
        .pipe(connect.reload());
});

// 编译js
gulp.task('js', function () {
    return gulp.src('src/**/*.js')
        .pipe(gulp.dest('dist'))
        .pipe(connect.reload());
});

// 编译img
gulp.task('img', function () {
    return gulp.src('src/img/**')
        .pipe(gulp.dest('dist/img'))
        .pipe(connect.reload());
});

// 自动监视
gulp.task('watch', function () {
    watch('src/**/*.html', function () {
        gulp.start(['html']);
    });
    watch('src/**/*.scss', function () {
        gulp.start(['sass']);
    });
    watch('src/**/*.js', function () {
        gulp.start(['js'])
    });
});

gulp.task('clean', function (cb) {
    return gulp.src('dist/')
        .pipe(clean());
});

gulp.task('dev', ['clean'], function () {
    gulp.start([
        'html',
        'sass',
        'js',
        'img',
        'webserver',
        'watch'
    ]);
})

gulp.task('default', ['dev']);